package cn.edu.zjgsu.ito.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class AdminExamService {

	public List<Record> index(){
		List<Record> testInf = Db.find("select * from test,teacher_info,course where test.tNo=teacher_info.tNo and test.courseNo=course.courseNo");
		for(int i = 0;i < testInf.size();i++){
			String tNames = "";
			if(testInf.get(i).getStr("Invigilation_tNo")!= null)
			{
				String[] tNoes = testInf.get(i).getStr("Invigilation_tNo").split(",");
				for(int j = 0;j < tNoes.length;j++){
					String tName = Db.findFirst("select * from teacher_info where tNo=?",tNoes[j]).getStr("tName");
					tNames += tName + ",";
				}
				int length = tNames.length();
				String temp = tNames.substring(0, length -1);
				testInf.get(i).set("tNames",temp);
			}
			if(testInf.get(i).getStr("testType").equals("1")){
				testInf.get(i).set("testType","正考");
			}else if(testInf.get(i).getStr("testType").equals("2")){
				testInf.get(i).set("testType","补考");
			}
		}
		return testInf;
	}

	//所有学期
	public List<Record> term(){
		List<Record> term = Db.find("select * from term");
		return term;
	}

	//按学期查找
	public List<Record> termSelect(String term,String courseNo) {
		List<Record> temp = null;
		if(term.equals("0") && courseNo.equals("0")){
			temp = Db.find("select * from test,teacher_info,course where test.tNo=teacher_info.tNo and test.courseNo=course.courseNo");
		}else if(term.equals("0") && !courseNo.equals("0")){
			temp = Db.find("select * from test,teacher_info,course where test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and test.courseNo=?",courseNo);
		}else if(!term.equals("0") && courseNo.equals("0")){
			temp = Db.find("select * from test,teacher_info,course where test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and termValue=?",term);
		}else{
			temp = Db.find("select * from test,teacher_info,course where test.tNo=teacher_info.tNo and test.courseNo=course.courseNo and termValue=? and test.courseNo=?",term,courseNo);
		}
		for(int i = 0;i < temp.size();i++){
			String tNames = "";
			if(temp.get(i).getStr("Invigilation_tNo")!= null)
			{
				String[] tNoes = temp.get(i).getStr("Invigilation_tNo").split(",");
				for(int j = 0;j < tNoes.length;j++){
					String tName = Db.findFirst("select * from teacher_info where tNo=?",tNoes[j]).getStr("tName");
					tNames += tName + ",";
				}
				int length = tNames.length();
				String temps = tNames.substring(0, length -1);
				temp.get(i).set("tNames",temps);
			}else{
				temp.get(i).set("tNames","");
			}
			if(temp.get(i).getStr("testType").equals("1")){
				temp.get(i).set("testType","正考");
			}else if(temp.get(i).getStr("testType").equals("2")){
				temp.get(i).set("testType","补考");
			}
		}
		return temp;
	}

	public void setTestInf(String test, String tNo, String classroom,
			String time) {
		String testNo[] = test.split(",");
		for(int i = 0;i < testNo.length;i++){
			Db.update("update test set Invigilation_tNo=?,test_classroomNo=?,test_time=? where testNo=?",tNo,classroom,time,testNo[i]);
		}
	}

	//创建补考
	public int addTest(String uuidArray){
		String[] uuid = uuidArray.split(",");
		int error = 0;
		System.out.println("数组长度" +  uuid.length);
		for(int j = 0;j < uuid.length; j++){
			List<Record> repecrt = Db.find("select * from test where uuid=?",uuid[j]);
			if(repecrt.size() < 2){
				if(repecrt.get(0).getStr("testLock").equals("1")){

					System.out.println("teskLock====1");

					Random ran=new Random();
					int a =ran.nextInt(99999999);
					String str = "";
					for (int i = 0; i < 10; i++) {// 你想生成几个字符的，就把4改成几，如果改成1,那就生成一个随机字母．
						str = str + (char) (Math.random() * 26 + 'a');
					}
					int b =ran.nextInt(99999999);
					String testNo = a + str + b;
					Record test = Db.findFirst("select * from test where uuid=?",uuid[j]);
					String termValue = test.getStr("termValue");
					String courseNo = test.getStr("courseNo");
					String tNo = test.getStr("tNo");
					Record addRepTest = new Record().set("termValue",termValue).set("courseNo",courseNo).set("tNo",tNo)
							.set("uuid",uuid[j]).set("testType",2).set("testLock",0).set("testNo",testNo);

					Record courseNa = Db.findFirst("select * from course where courseNo=?",courseNo);
					String courseName = courseNa.getStr("courseName");
					Record courseTemp = new Record().set("termValue",termValue).set("courseNo",courseNo).set("courseName",courseName)
							.set("endDate","未设置截止时间").set("testNo",testNo);
					Db.save("course_end",courseTemp);
					Db.save("test",addRepTest);

					//自动添加学生
					produceRepGra(uuid[j],testNo);
				}else{
					error += 1;
					System.out.println("不符合添加补考。");
				}
			}else{
				error += 1;
				System.out.println("已经生成了该门考试。");
			}
		}
		if(error == 0){
			return 0;
		}else{
			return 1;
		}
	}

	//生成补考名单
	public void produceRepGra(String uuid,String testNo){
		System.out.println("添加学生");
		List<Record> stuGraIf = Db.find("select * from student_grade,student_info where student_grade.passportNo=student_info.passportNo and uuid=?",uuid);

		if(stuGraIf.size() == 0){
			System.out.println("没有学生成绩，无法添加考试。");
			return;
		}

		for(int i = 0;i < stuGraIf.size();i++){
			String grade = stuGraIf.get(i).getStr("grade");
			System.out.println("====");
			if(grade.equals("A")||grade.equals("B")||grade.equals("C")||grade.equals("P")||grade.equals("F")){
				if( !grade.equals("F") ){
					stuGraIf.remove(i);
					i--;
				}
			}else{
				if(Integer.valueOf(grade) >= 60){
					stuGraIf.remove(i);
					i--;
				}
			}
		}
		if(stuGraIf.size() > 0){
			System.out.println("有不及格的人");
			for(int i = 0;i < stuGraIf.size();i++){
				String termValue = stuGraIf.get(i).getStr("termValue");
				String majorNo = stuGraIf.get(i).getStr("majorNo");
				String majorName = stuGraIf.get(i).getStr("majorName");
				String courseNo = stuGraIf.get(i).getStr("courseNo");
				String courseName = stuGraIf.get(i).getStr("courseName");
				String passportNo = stuGraIf.get(i).getStr("passportNo");
				String tNo = stuGraIf.get(i).getStr("tNo");

				Record repStu = new Record().set("termValue",termValue).set("passportNo",passportNo).set("majorNo",majorNo)
						.set("majorName",majorName).set("courseNo",courseNo).set("courseName",courseName)
						.set("tNo",tNo).set("gradeStatus",0).set("uuid",uuid).set("testNo",testNo);
				Db.save("student_grade", repStu);
			}
		}
	}

	//获取教师
	public List<Record> teacher() {
		List<Record> teacher = Db.find("select * from teacher_info");
		return teacher;
	}

	//获取教室
	public List<Record> classroom() {
		List<Record> classroom = Db.find("select * from classroom");
		return classroom;
	}
	
	//获取教室
	public List<Record> classroomAjax(String classroomType) {
		List<Record> classroom = null;
		if(classroomType.equals("全部")){
			classroom = Db.find("select * from classroom");
		}else{
			classroom = Db.find("select * from classroom where classroomType=?",classroomType);
		}
		return classroom;
	}

	public List<Record> course() {
		List<Record> course = Db.find("select * from course");
		return course;
	}

	//获取学院
	public List<Record> college() {
		List<Record> college = Db.find("select * from college");
		return college;
	}

	public List<Record> classroomType() {
		List<Record> classroom = Db.find("select * from classroom group by classroomType");
		return classroom;
	}

}
